<template>
    <div class="app-container content-wrapper">
        <div class="filter-container">
            <el-form :model="listQuery" :inline="true" ref="filterForm" style="display: inline-block">
                <el-form-item prop="carFullNum" class="filter-item">
                    <el-input v-model="listQuery.carFullNum" placeholder="车牌号" style="width: 200px;"></el-input>
                </el-form-item>

                <el-form-item prop="limitTime" class="filter-item">
                    <el-select v-model="listQuery.limitTime" placeholder="保养期限" clearable style="width: 150px">
                        <el-option
                                v-for="item in limitOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-button class="filter-item" type="primary" icon="search" @click="handleFilter">搜索</el-button>
                <el-button class="filter-item" type="primary" icon="mars-reset" @click="handleResetFilter">重置</el-button>
            </el-form>
        </div>

        <el-table :data="list" v-loading="listLoading"
                  element-loading-text="给我一点时间"
                  border fit highlight-current-row
                  @cell-click="handleCellClick"
                  style="width: 100%">

            <el-table-column label="ID" width="65" align="center">
                <template slot-scope="scope"> {{scope.row.id}} </template>
            </el-table-column>

            <el-table-column label="车牌号" width="300px" align="center">
                <template slot-scope="scope"> {{scope.row.carFullNum}} </template>
            </el-table-column>

            <el-table-column label="车型" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.carName}} </template>
            </el-table-column>

            <el-table-column label="当前里程数" align="center">
                <template slot-scope="scope"> {{scope.row.carCurrMileage}} </template>
            </el-table-column>

            <el-table-column label="下次保养里程数" align="center">
                <template slot-scope="scope">
                    <el-tag type="primary" v-if="scope.row.carNextRepair != null">{{scope.row.carNextRepair}}</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="是否到期保养" align="center">
                <template slot-scope="scope">
                    <el-tag type="danger" v-if="scope.row.carCurrMileage >= scope.row.carNextRepair">已到期</el-tag>
                    <el-tag type="success" v-else>未到期</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="280px" align="center">
                <template slot-scope="scope">
                    <el-button size="small" type="primary" @click="handleUpdate(scope.row)">设定下次保养</el-button>
                    <el-button size="small" type="primary" @click="handleDetail(scope.row)">保养日志</el-button>
                </template>
            </el-table-column>

        </el-table>

        <div v-show="!listLoading" class="pagination-container">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.page"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="listQuery.rows"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <el-dialog title="设定下次保养里程数" :visible.sync="dialogFormVisible">
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="里程数" prop="carNextRepair">
                    <el-input v-model="form.carNextRepair" style="width: 400px;"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="update">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    import fetch from '@/utils/fetch'
    import { saveOrUpdate, fetchList } from '@/api/tukuan/car_repair'

    const limitOptions = [{label: '已到期', key: true}, {label: '未到期', key: false}]

    export default {
        name: 'app',
        data() {
            return {
                list: null,
                total: null,
                listLoading: true,
                listQuery: {
                    page: 1,
                    rows: 10,
                    carNum: undefined,
                    limitTime: undefined,
                    orderBy: 'create_time desc'
                },
                limitOptions,
                dialogFormVisible: false,
                form: {
                    carId: undefined,
                    carNextRepair: undefined
                },
                rules: {
                    carNextRepair: [
                        { required: true, message: '请输入里程数'},
                        { message: '请输入数字', pattern: /^[0-9]+.?[0-9]*$/ }
                    ],
                }
            }
        },
        created() {
            this.getList()
        },
        methods: {
            handleDetail(row) {
                if(row.repairId) {
                    this.$router.push({'path': `/car/repair-log/${row.repairId}/${row.carFullNum}`})
                } else {
                    this.$notify({
                        title: '提示', message: '未有保养记录，请先初始化“下次保养里程数”',
                        type: 'error', duration: 2000
                    })
                }

            },
            getList() {
                this.listLoading = true
                fetchList(this.listQuery).then(response => {
                    const items = response.data.list
                    this.list = items
                    this.total = response.data.total
                    this.listLoading = false
                }).catch((error)=>{
                    console.log(error)
                    this.listLoading = false
                })
            },
            handleCellClick(row, column, cell, event) {
            },
            handleSizeChange(val) {
                this.listQuery.rows = val
                this.getList()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getList()
            },
            handleCloseDialog() {
                this.resetForm()
            },
            handleFilter() {
                this.listQuery.page = 1
                this.getList()
            },
            handleResetFilter() {
                this.$refs.filterForm.resetFields()
                this.getList()
            },
            handleUpdate(row) {//修改数据窗口
                this.dialogFormVisible = true

                this.form.id = row.id
                this.form.carNextRepair = row.carNextRepair
            },
            update() {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        saveOrUpdate(this.form).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getList()
                                this.dialogFormVisible = false
                            } else {
                                this.$notify({
                                    title: '发生错误', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                        })
                    }
                })

            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>
</style>